package com.petecat.interchan.base.param.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.mhuang.common.util.DataUtils;
import com.petecat.interchan.base.param.entity.BsProperties;
import com.petecat.interchan.base.param.mapper.IParamMapper;
import com.petecat.interchan.base.param.service.IParamService;
import com.petecat.interchan.base.project.entity.BsProject;
import com.petecat.interchan.core.exception.BusinessException;
import com.petecat.interchan.core.service.impl.BaseServiceImpl;
import com.petecat.interchan.protocol.Result;
import com.petecat.interchan.protocol.base.param.dto.BsPropertiesAddDTO;
import com.petecat.interchan.protocol.base.param.dto.BsPropertiesQryDTO;
import com.petecat.interchan.protocol.base.param.dto.BsPropertiesUpdDTO;
import com.petecat.interchan.protocol.base.param.vo.BsPropertiesQryVO;
import com.petecat.interchan.protocol.base.project.vo.BsProjectQryVO;
import com.petecat.interchan.protocol.data.Page;
import com.petecat.interchan.protocol.data.PageVO;
import com.petecat.interchan.protocol.sso.sysuser.SysUserAddDTO;
import com.petecat.interchan.redis.IdWorker;
import com.petecat.interchan.redis.commands.IRedisExtCommands;

@Service
public class ParamServiceImpl extends BaseServiceImpl<BsProperties, String>
								implements IParamService{
	
	@Autowired
	private IParamMapper paramMapper;
	
	@Autowired
	private IRedisExtCommands redisExtCommands;
	
	@Autowired
	private Environment environment;
	
	private final static String SYUSER_REDIS_USERID_TO_USER_PREKEY="syur_user_uid_";

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public void modifyParamById(BsPropertiesUpdDTO bsPropertiesUpdDTO, String userId) {
		BsProperties bsProperties = DataUtils.copyTo(bsPropertiesUpdDTO, BsProperties.class);
		bsProperties.setOperaorUserId(userId);
		bsProperties.setOperatorTime(new Date());
		this.paramMapper.update(bsProperties);
	}

	/**
	 * 先停用所用,最后启用一个  配置只能启用一个
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public void deleteParamById(String paramId,String projectId, String state) {
		//停用所有项目配置
		this.paramMapper.updateStateByProjectId(projectId);
		//启用其中一个配置
		this.paramMapper.updateCompanyStateById(paramId, state);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public void saveParam(BsPropertiesAddDTO bsPropertiesAddDTO, String userId) {
		BsProperties bsProperties = DataUtils.copyTo(bsPropertiesAddDTO, BsProperties.class);
		bsProperties.setCreateTime(new Date());
		bsProperties.setOperatorTime(new Date());
		bsProperties.setId(String.valueOf(new IdWorker().nextId()));
		bsProperties.setOperaorUserId(userId);
		bsProperties.setUserId(userId);
		bsProperties.setStatus("0");
		this.paramMapper.save(bsProperties);
	}

	@Override
	public PageVO<BsPropertiesQryVO> queryParamByPage(BsPropertiesQryDTO bsPropertiesQryDTO) {
		SysUserAddDTO sysUserAddDTO = redisExtCommands.hget(SYUSER_REDIS_USERID_TO_USER_PREKEY, bsPropertiesQryDTO.getUserId(), SysUserAddDTO.class);
		if(null == sysUserAddDTO) {
			throw new BusinessException(Result.SYS_RESULT_FAILD,
					environment.getProperty("user_not_exites"));
		}
		String user = "0".equals(sysUserAddDTO.getIsMaster()) ? sysUserAddDTO.getUserid():null;
		bsPropertiesQryDTO.setUserId(user);
		BsProperties bsProperties = DataUtils.copyTo(bsPropertiesQryDTO, BsProperties.class);
		int count = this.paramMapper.count(bsProperties);
		PageVO<BsPropertiesQryVO> pageVo = new PageVO<>();
		if(count > 0) {
			Page<BsProperties> page = new Page<>();
			page.setRows(bsPropertiesQryDTO.getRows());
			page.setStart(bsPropertiesQryDTO.getStart());
			page.setRecord(bsProperties);
			List<BsProperties> companyList = this.paramMapper.page(page);
			pageVo.setResult(DataUtils.copyTo(companyList, BsPropertiesQryVO.class));
		}
		pageVo.setTotalSize(count);
		return pageVo;
	}

	/**
	 * 删除参数配置
	 */
	@Override
	public void removeParamById(String paramId) {
		this.paramMapper.remove(paramId);
	}
	
	/**
	 * 通过项目ID和用户ID查询正在启用的配置信息
	 */
	public BsPropertiesQryVO getParamByProjectIdAndUserId(String projectId,String userId) {
		return this.paramMapper.getParamByProjectIdAndUserId(projectId,userId);
	}
	
}
